package Servlet;


import Bean.Flight;
import Bean.Page;
import Dao.FlightDao;
import Service.FlightService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张
 * Date: 2025-03-08
 * Time: 18:28
 */
//查询航班信息(指定数据)
@WebServlet("/SelectFlightServlet")
public class SelectFlightServlet extends HttpServlet {
    private FlightService flightService = new FlightService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=utf8");
        //接收数据 起飞机场,降落机场,时间
        String start = req.getParameter("startLocation");
        String end = req.getParameter("endLocation");
        String startTime = req.getParameter("startTime");
        int pageNo = Integer.parseInt(req.getParameter("pageNo"));
        int pageSize = 5;
        Page page = flightService.selectFlights(pageNo,pageSize,start,end,startTime);
        System.out.println(page.getList().toString());

        if(page.getTotalPages() == 0) {
            req.setAttribute("status", "error");
            req.setAttribute("message", "所查询的航行暂无!!");
            req.setAttribute("redirectUrl", "user_index.jsp");
            req.setAttribute("waitSeconds", 2);
            req.getRequestDispatcher("skip.jsp").forward(req,resp);
        }
        //在传参的时候要把start,end,startTime进行回传
        req.setAttribute("start",start);
        req.setAttribute("end",end);
        req.setAttribute("startTime",startTime);
        req.setAttribute("list",page.getList());
        req.setAttribute("box",page);
        req.getRequestDispatcher("flight_list.jsp").forward(req,resp);
    }
}
